package com.baobao.web;

import com.baobao.dao.ParentDAO;

import com.baobao.domain.Parent;

import com.baobao.service.ParentService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.servlet.ModelAndView;

/**
 * Spring MVC controller that handles CRUD requests for Parent entities
 * 
 */

@Controller("ParentController")
public class ParentController {

	/**
	 * DAO injected by Spring that manages Parent entities
	 * 
	 */
	@Autowired
	private ParentDAO parentDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for Parent entities
	 * 
	 */
	@Autowired
	private ParentService parentService;

	/**
	 * Show all Parent entities
	 * 
	 */
	@RequestMapping("/indexParent")
	public ModelAndView listParents() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("parents", parentService.loadParents());

		mav.setViewName("parent/listParents.jsp");

		return mav;
	}

	/**
	 * Entry point to show all Parent entities
	 * 
	 */
	public String indexParent() {
		return "redirect:/indexParent";
	}

	/**
	 * Save an existing Parent entity
	 * 
	 */
	@RequestMapping("/saveParent")
	public String saveParent(@ModelAttribute Parent parent) {
		parentService.saveParent(parent);
		return "forward:/indexParent";
	}

	/**
	 * Select the Parent entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteParent")
	public ModelAndView confirmDeleteParent(@RequestParam String phoneKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("parent", parentDAO.findParentByPrimaryKey(phoneKey));
		mav.setViewName("parent/deleteParent.jsp");

		return mav;
	}

	/**
	 * Select an existing Parent entity
	 * 
	 */
	@RequestMapping("/selectParent")
	public ModelAndView selectParent(@RequestParam String phoneKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("parent", parentDAO.findParentByPrimaryKey(phoneKey));
		mav.setViewName("parent/viewParent.jsp");

		return mav;
	}

	/**
	 * Register custom, context-specific property editors
	 * 
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	 * Create a new Parent entity
	 * 
	 */
	@RequestMapping("/newParent")
	public ModelAndView newParent() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("parent", new Parent());
		mav.addObject("newFlag", true);
		mav.setViewName("parent/editParent.jsp");

		return mav;
	}

	/**
	 */
	@RequestMapping("/parentController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request, @ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

	/**
	 * Delete an existing Parent entity
	 * 
	 */
	@RequestMapping("/deleteParent")
	public String deleteParent(@RequestParam String phoneKey) {
		Parent parent = parentDAO.findParentByPrimaryKey(phoneKey);
		parentService.deleteParent(parent);
		return "forward:/indexParent";
	}

	/**
	 * Edit an existing Parent entity
	 * 
	 */
	@RequestMapping("/editParent")
	public ModelAndView editParent(@RequestParam String phoneKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("parent", parentDAO.findParentByPrimaryKey(phoneKey));
		mav.setViewName("parent/editParent.jsp");

		return mav;
	}
}